package com.yuan.algorithms.practice201505;

import java.math.BigInteger;
import java.util.Scanner;


public class 大数运算 {

	static int[] arr1;
	static int[] arr2;
	static int[] result;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			arr1 = new int[1000000];
			arr2 = new int[1000000];
			result = new int[1000000];
			String a,b;
			System.out.println("请选择要进行的运算：");
			System.out.println("1.加法\t2.减法\n3.除法\t4.乘法");
			char order = sc.next().charAt(0);
			switch (order) {
			case '1':
				System.out.println("请输入要相加的两个数：");
				a = sc.next();
				b = sc.next();
				System.out.println(add(a,b));
				break;
			case '2':
				System.out.println("请输入要相减的两个数：");
				a = sc.next();
				b = sc.next();
				BigInteger bia = new BigInteger(a);
				bia = bia.add(new BigInteger(b));
				System.out.println(bia.toString());
				break;
			case '3':
				System.out.println("请输入要相乘的两个数：");
				a = sc.next();
				b = sc.next();
				System.out.println(multiply(a,b));
				break;
			case '4':
				System.out.println("请输入要相除的两个数：");
				a = sc.next();
				b = sc.next();
				System.out.println(divide(a,b));
				break;
			default:
				System.out.println("你的选择有误，请重新选择...");
				break;
			}
		}
	}

	private static String divide(String a, String b) {
		change(a, b);
		
		return null;
	}


	private static String multiply(String a, String b) {
		change(a, b);
		return null;
	}

	private static String subtract(String a, String b) {
		change(a, b);
		return null;
	}

	private static String add(String a, String b) {
		change(a, b);
		String k = "";
		for (int i = arr1.length-1; i >= 0; i--) {
			result[i] = arr1[i] + arr2[i]; 
			if (result[i]>9) {
				result[i] %= 9;
				result[i-1] += result[i] / 10;
			}
		}
		for (int i = 0, j = 0; i < result.length; i++) {
			if (result[i] != 0 || j!=0) {
				j = 1;
				k += result[i];
			}
			
		}
		return k;
	}
	
	private static void change(String a, String b) {
		for (int i = 0, k = result.length; i < result.length; i++) {
			if (i<a.length()) {
				arr1[--k] = a.charAt(i) - '0'; 
			}
			if (i<b.length()) {
				arr2[--k] = b.charAt(i) - '0'; 
			}
			if (i>=a.length() && i>=b.length()) {
				break;
			}
		}
	}

	private static String factorial(int k) {
		
		return null;
	}
}
